package com.xiaol.springbootredis.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * User: Ray.Shaw <p />
 * Time: 2018/7/27 16:46  <p />
 * Version: V1.0  <p />
 * Description:  <p />
 */
@Service
public class RedisOptionService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 保存String数据
     *
     * @Title: setStr
     * @param key
     * @param value
     *
     */
    public void setStr(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public String getString(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    /**
     * 操作对象
     *
     * @Title: setObject
     * @param key
     * @param obj
     *
     */
    public void setObject(String key, Object obj) {
        redisTemplate.opsForValue().set(key, obj);
    }

    public Object getObject(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 保存List数据存储
     *
     * @param key
     * @param listValue
     * @return
     */
    public void setList(String key, List<?> listValue) {
        redisTemplate.opsForList().leftPush(key, listValue);
    }

    public List<?> getList(String key) {
        return (List<?>) redisTemplate.opsForList().range(key, 0, -1);
    }

    /**
     * 保存Map数据存储
     *
     * @param key
     * @param mapValue
     * @return
     */
    public void setMap(String key, Map<Object, Object> mapValue) {
        redisTemplate.opsForHash().putAll(key, mapValue);
    }

    public Map<Object, Object> getMap(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 保存Set数据存储
     *
     * @param key
     * @param setValue
     * @return
     */
    public void setSet(String key, Set<?> setValue) {
        redisTemplate.opsForSet().add(key, setValue);
    }

    public Set<?> getSet(String key) {
        return (Set<?>) redisTemplate.opsForSet().members(key);
    }
}
